﻿using Logic;
using System;
using UnityEngine;

namespace Level.Decorations
{
    public class ObstacleHealth : MonoBehaviour, IHealth
    {
        public event Action HealthDecreased;
        public event Action Destroyed;

        public float Max { get; set; }

        public float Current
        {
            get => _current;
            set
            {
                _current = value;

                if (_current <= 0) 
                    Die();
            }
        }

        private float _current;

        private bool _destroyed;

        private void Start() => 
            _current = Max;

        public void TakeDamage(float damage)
        {
            if (_destroyed) return;
            Current -= damage;
            HealthDecreased?.Invoke();
        }

        public void Die()
        {
            _destroyed = true;
            Destroyed?.Invoke();
        }
    }
}